package com.showde.shoppingmall.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TabHost;
import android.widget.TextView;

import com.showde.shoppingmall.R;
import com.showde.shoppingmall.db.ShoppingcartDBCommand;

@SuppressWarnings("deprecation")
public class TabMainActivity extends android.app.TabActivity implements
		View.OnClickListener {

	private TabHost tabhost;
	private Intent intent1, intent2, intent3, intent4, intent5;

	private RadioButton tabMain, tabCategory, tabRecommend, tabShoppingcart,
			tabMore;

	private TextView shoppingcartCount;

	ShoppingcartDBCommand shoppingcartDBCommand;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_tab);
		initTab();
		initRadioButton();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (shoppingcartDBCommand == null) {
			shoppingcartDBCommand = new ShoppingcartDBCommand(this);
		}
		int count = shoppingcartDBCommand.getShoppingcartCount();
		if (count > 0) {
			shoppingcartCount.setText(String.valueOf(count));
			shoppingcartCount.setVisibility(View.VISIBLE);
		} else {
			shoppingcartCount.setVisibility(View.GONE);
		}
	}

	private void initRadioButton() {
		tabMain = (RadioButton) findViewById(R.id.tab_home);
		tabRecommend = (RadioButton) findViewById(R.id.tab_recommend);
		tabCategory = (RadioButton) findViewById(R.id.tab_category);
		tabShoppingcart = (RadioButton) findViewById(R.id.tab_shoppingcart);
		tabMore = (RadioButton) findViewById(R.id.tab_more);
		tabMain.setOnClickListener(this);
		tabRecommend.setOnClickListener(this);
		tabCategory.setOnClickListener(this);
		tabShoppingcart.setOnClickListener(this);
		tabMore.setOnClickListener(this);
		tabMain.setBackgroundResource(R.drawable.navigation_main_pressed);
	}

	private void initTab() {

		tabhost = getTabHost();

		intent1 = new Intent(TabMainActivity.this, MyStreetGroup.class);
		tabhost.addTab(tabhost.newTabSpec("one").setIndicator("one")
				.setContent(intent1));
		intent2 = new Intent(TabMainActivity.this, RecommendGroup.class);
		tabhost.addTab(tabhost.newTabSpec("two").setIndicator("two")
				.setContent(intent2));
		intent3 = new Intent(TabMainActivity.this, CategoryGroup.class);
		tabhost.addTab(tabhost.newTabSpec("three").setIndicator("three")
				.setContent(intent3));
		intent4 = new Intent(TabMainActivity.this, ShoppingcarGroup.class);
		tabhost.addTab(tabhost.newTabSpec("four").setIndicator("four")
				.setContent(intent4));
		intent5 = new Intent(TabMainActivity.this, MoreGroup.class);
		tabhost.addTab(tabhost.newTabSpec("five").setIndicator("five")
				.setContent(intent5));

		shoppingcartCount = (TextView) findViewById(R.id.main_tab_setting_new_tv);

	}

	@Override
	public void onClick(View v) {
		int viewId = v.getId();
		changeBackground(viewId);
		switch (viewId) {
		case R.id.tab_home:
			tabhost.setCurrentTab(0);
			break;
		case R.id.tab_recommend:
			tabhost.setCurrentTab(1);
			break;
		case R.id.tab_category:
			tabhost.setCurrentTab(2);
			break;
		case R.id.tab_shoppingcart:
			tabhost.setCurrentTab(3);
			break;
		case R.id.tab_more:
			tabhost.setCurrentTab(4);
			break;
		}

	}

	private void changeBackground(int viewId) {

		tabMain.setBackgroundResource(R.drawable.navigation_main);
		tabCategory.setBackgroundResource(R.drawable.navigation_category);
		tabRecommend.setBackgroundResource(R.drawable.navigation_recommend);
		tabShoppingcart
				.setBackgroundResource(R.drawable.navigation_shoppingcar);
		tabMore.setBackgroundResource(R.drawable.navigation_more);

		switch (viewId) {
		case R.id.tab_home:
			tabMain.setBackgroundResource(R.drawable.navigation_main_pressed);
			break;
		case R.id.tab_category:
			tabCategory
					.setBackgroundResource(R.drawable.navigation_category_pressed);
			break;
		case R.id.tab_recommend:
			tabRecommend
					.setBackgroundResource(R.drawable.navigation_recommend_pressed);
			break;
		case R.id.tab_shoppingcart:
			tabShoppingcart
					.setBackgroundResource(R.drawable.navigation_shoppingcar_pressed);
			break;
		case R.id.tab_more:
			tabMore.setBackgroundResource(R.drawable.navigation_more_pressed);
			break;
		}
	}

}
